package com.ruoyi.ring.service.impl;

import cn.hutool.core.convert.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.ring.domain.RingHeartRate;
import com.ruoyi.ring.domain.RingHeartRateInfo;
import com.ruoyi.ring.domain.dto.RingHeartRateDTO;
import com.ruoyi.ring.domain.vo.RingHeartRateVO;
import com.ruoyi.ring.mapper.RingHeartRateInfoMapper;
import com.ruoyi.ring.mapper.RingHeartRateMapper;
import com.ruoyi.ring.service.IRingHeartRateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Stream;

/**
 * 心率Service业务层处理
 * 
 * @author liuwp
 * @date 2025-08-21
 */
@Service
public class RingHeartRateServiceImpl implements IRingHeartRateService 
{
    @Resource
    private RingHeartRateMapper ringHeartRateMapper;
    @Resource
    private RingHeartRateInfoMapper ringHeartRateInfoMapper;

    /**
     * 查询心率
     * 
     * @param id 心率主键
     * @return 心率
     */
    @Override
    public RingHeartRate selectRingHeartRateById(Long id)
    {
        return ringHeartRateMapper.selectRingHeartRateById(id);
    }

    /**
     * 查询心率列表
     * 
     * @param ringHeartRate 心率
     * @return 心率
     */
    @Override
    public List<RingHeartRate> selectRingHeartRateList(RingHeartRate ringHeartRate)
    {
        return ringHeartRateMapper.selectRingHeartRateList(ringHeartRate);
    }

    /**
     * 根据业务主键查询一条记录：心率
     *
     * @param ringHeartRate 心率
     * @return 心率
     */
    @Override
    public RingHeartRate selectOneRingHeartRate(RingHeartRate ringHeartRate)
    {
        return ringHeartRateMapper.selectOneRingHeartRate(ringHeartRate);
    }

    /**
     * 新增心率
     * 
     * @param ringHeartRate 心率
     * @return 结果
     */
    @Override
    public int insertRingHeartRate(RingHeartRate ringHeartRate)
    {
        ringHeartRate.setCreateTime(DateUtils.getNowDate());
        return ringHeartRateMapper.insertRingHeartRate(ringHeartRate);
    }

    /**
     * 修改心率
     * 
     * @param ringHeartRate 心率
     * @return 结果
     */
    @Override
    public int updateRingHeartRate(RingHeartRate ringHeartRate)
    {
        ringHeartRate.setUpdateTime(DateUtils.getNowDate());
        return ringHeartRateMapper.updateRingHeartRate(ringHeartRate);
    }

    @Override
    public int updateRingHeartRateCondition(RingHeartRate ringHeartRate, RingHeartRate ringHeartRateCondition)
    {
                ringHeartRate.setUpdateTime(DateUtils.getNowDate());
        return ringHeartRateMapper.updateRingHeartRateCondition(ringHeartRate, ringHeartRateCondition);
    }

    /**
     * 批量删除心率
     * 
     * @param ids 需要删除的心率主键
     * @return 结果
     */
    @Override
    public int deleteRingHeartRateByIds(Long[] ids)
    {
        return ringHeartRateMapper.deleteRingHeartRateByIds(ids);
    }

    /**
     * 删除心率信息
     * 
     * @param id 心率主键
     * @return 结果
     */
    @Override
    public int deleteRingHeartRateById(Long id)
    {
        return ringHeartRateMapper.deleteRingHeartRateById(id);
    }

    /**
     * 条件删除心率
     *
     * @param ringHeartRate 心率
     * @return 结果
     */
    @Override
    public int deleteRingHeartRateByCondition(RingHeartRate ringHeartRate)
    {
        return ringHeartRateMapper.deleteRingHeartRateByCondition(ringHeartRate);
    }

    @Override
    public RingHeartRateVO queryRingHeartRate(RingHeartRateDTO ringHeartRateDTO) {
        RingHeartRateVO heartRateVO = new RingHeartRateVO();
        RingHeartRate ringHeartRate = ringHeartRateMapper.queryOneRingHeartRate(ringHeartRateDTO);
        if(ringHeartRate != null){
            heartRateVO =Convert.convert(RingHeartRateVO.class,ringHeartRate);
            RingHeartRateInfo ringHeartRateInfo = ringHeartRateInfoMapper.selectOneRingHeartRateInfo(Builder.of(RingHeartRateInfo::new).with(RingHeartRateInfo::setDailyValue, heartRateVO.getDailyValue()).build());
            if(ringHeartRateInfo != null){
                String[] rateInfos = ringHeartRateInfo.getHeartRateList().split(",");//转成String数组
                int[] rates = Stream.of(rateInfos).mapToInt(Integer::parseInt).toArray();//转成int数组
                heartRateVO.setHeartRateList(rates);
            }
        }
        return heartRateVO;
    }

    @Override
    public List<RingHeartRate> queryRingHeartRateList(RingHeartRateDTO ringHeartRateDTO) {
        return ringHeartRateMapper.queryRingHeartRateList(ringHeartRateDTO);
    }

}
